﻿<html>
<head>
<style>
body {
  min-width:500px;
  overflow-x:hidden;
}

table {
  font-size: 13px;
  font-family: Verdana, Arial, Helvetica, sans-serif;
  line-height: 20px;
  color: #4A4949;
}

div.text{
  font-size: 13px;
  font-family: Verdana, Arial, Helvetica, sans-serif;
  line-height: 20px;
  color: #4A4949;
}

div.link{
  font-size: 13px;
  font-family: Verdana, Arial, Helvetica, sans-serif;
  line-height: 20px;
  color: #4A4949;
  cursor: pointer;
  text-decoration: underline;
}

span.link{
  font-size: 13px;
  font-family: Verdana, Arial, Helvetica, sans-serif;
  line-height: 20px;
  color: #4A4949;
  cursor: pointer;
  text-decoration: underline;
}

img {
  margin:5px;
  border:2px solid black;
  vertical-align:middle;
  width:100px;
  height:75px;
}
</style>
<script type="text/javascript" src="util.js"></script>
<script type="text/javascript" src="IgnoreList.js"></script>
<script type="text/javascript" src="dromruparser.js"></script>
<script>
var urls = loadUrls();
var ignored = new IgnoreList();
window.setTimeout(function() {
      printData();
    }, 500);

function compareCars(a, b)
{
    return b.id - a.id;
}

function markAsRead()
{
    for (var i = 0; i < urls.length; ++i){
        for (var ii = 0; ii < urls[i].data.length; ++ii){
            urls[i].data[ii].watched = true;
        }
    }
    saveUrls(urls);
    chrome.browserAction.setBadgeText({text: ""});
}

function hideAdRow(id)
{
    var table = document.getElementById("adverts");
    var row = document.getElementById("ad_" + id);
    table.removeChild(row);
}

function ignoreModel(model, year, id)
{
    hideAdRow(id);
    
    if (ignored.isIgnored(model, year))
        return;
    ignored.ignore(model, year);
}

function printData()
{
    if (urls.length == 0) {
        return;
    }
    ignored.load();
    var count = getUnwatchedCount(urls);
    if (count == 0)
    {
        var a = document.createElement("div");
        a.setAttribute('class', "text");
        a.appendChild(document.createTextNode("Нет новых объявлений"));
        document.body.appendChild(a);

        a = document.createElement("div");
        a.setAttribute('class', "text");
        var lastDate = getLastScanDate();
        a.appendChild(document.createTextNode(lastDate));
        document.body.appendChild(a);
        return;
    }
    var a = document.createElement("div");
    a.setAttribute('onClick', "markAsRead();");
    a.setAttribute('class', "link");
    a.appendChild(document.createTextNode("Пометить как прочитанные"));
    document.body.appendChild(a);
    
    var mainTable = document.createElement("table");
    mainTable.setAttribute('id', "adverts");
    document.body.appendChild(mainTable);
    for (var urlidx = 0; urlidx < urls.length; ++urlidx){
        var data = urls[urlidx].data;
        for (var i = 0; i < data.length; ++i){
            if (data[i].watched)
                continue;
            var row = document.createElement("tr");
            row.setAttribute('id', "ad_" + i);
            mainTable.appendChild(row);
            
            var cell = document.createElement("td");
            row.appendChild(cell);
            var a = document.createElement("div");
            a.setAttribute('class', "link");
            a.setAttribute('onClick', "chrome.tabs.create({'url':\"" + data[i].url + "\", 'selected':true});");
            a.appendChild(document.createTextNode(data[i].date));
            cell.appendChild(a);
            
            cell = document.createElement("td");
            row.appendChild(cell);
            var a = document.createElement("div");
            a.setAttribute('onClick', "chrome.tabs.create({'url':\"" + data[i].url + "\", 'selected':true});");
            var img = document.createElement("img");
            img.setAttribute('src', data[i].imgUrl);
            a.appendChild(img);
            cell.appendChild(a);
            
            cell = document.createElement("td");
            row.appendChild(cell);
            if (data[i].sold)
            {
                var strike = document.createElement("strike");
                cell.appendChild(strike);
                strike.appendChild(document.createTextNode(data[i].model));
            }
            else
            {
                cell.appendChild(document.createTextNode(data[i].model));
                a = document.createElement("div");
                a.setAttribute('class', "link");
                a.setAttribute('onClick', "ignoreModel('" + data[i].model.trim() + "', '" + data[i].year.trim() + "', '" + i + "');");
                a.appendChild(document.createTextNode("Игнорировать"));
                cell.appendChild(a);
            }
            cell.appendChild(document.createElement("br"));
            cell.appendChild(document.createTextNode(data[i].year + " | " + data[i].engine + " (" + data[i].fuel + ")"));
            cell.appendChild(document.createElement("br"));
            cell.appendChild(document.createTextNode(data[i].gearbox + " | " + data[i].drive));
            
            cell = document.createElement("td");
            row.appendChild(cell);
            cell.appendChild(document.createTextNode(data[i].price));
            cell.appendChild(document.createElement("br"));
            cell.appendChild(document.createTextNode(data[i].track));
            cell.appendChild(document.createElement("br"));
            cell.appendChild(document.createTextNode(data[i].city));
        }
    }
}

function printError(error)
{
    var message = document.createElement("p");
    var text = document.createTextNode(error);
    message.appendChild(text);
    document.body.appendChild(message);
}
</script>
</head>
<body>
</body>
</html>